/**
 * @author 老刘
 * @desc 天气
 */
import {wxApiPromise} from '../../../utils/util';
const amapFile = require('../../../libs/amap-wx.js'); // 引用amap-wx.js文件
Page({
	data: {
		showLoading: false,
		weather: [],
		markersData: {
			latitude: '',// 纬度
			longitude: '',// 经度
			key: '36da594ffe153906fbfbb1e0305347c4'// 申请的高德地图key
		},
		city: '',
		address: '',
		address2: ''
	},
	async onShow () {
		this.setData({
			showLoading: true
		});
		await this.loadInfo(); // 执行函数
	},
	// 获取当前位置的经纬度
	async loadInfo () {
		const info = await wxApiPromise(wx.getLocation, 'userLocation', this);
		this.loadCity(info.latitude, info.longitude);
	},
	// 把当前位置的经纬度传给高德地图，调用高德API获取当前地理位置，天气情况等信息
	loadCity (latitude, longitude) {
	const that = this;
	const myAmapFun = new amapFile.AMapWX({ key: that.data.markersData.key });
		myAmapFun.getRegeo({
			location: '' + longitude + ',' + latitude + '',// location的格式为'经度,纬度'
			success: (data) => {
				console.log('开始数据');
				console.log(data);
				that.setData({
					city: data[0]['regeocodeData']['addressComponent']['city'],
					address: data[0]['desc'],
					address2: data[0]['name']
				});
			},
			fail () { }
		});
		myAmapFun.getWeather({
			success: function (data) {
				that.setData({
					weather: data
				});
				console.log('开始数据2');
				console.log(data);
				// 成功回调
			},
			fail (info) {
				// 失败回调
				console.log('开始数据3');
				console.log(info);
			}
		});
	}
});
